﻿//百度地图
var map = new BMap.Map("bad_box");
map.centerAndZoom("重庆", 12);
map.enableScrollWheelZoom(true);
var opts = {
    width: 250,     // 信息窗口宽度
    height: 100,     // 信息窗口高度
    title: "车辆信息", // 信息窗口标题
    enableMessage: true//设置允许信息窗发送短息
};
// 编写自定义函数,创建标注
function addMarker(point, platesNumber, name, time) {
    $.ajax({
        url: "https://api.map.baidu.com/geocoder/v2/?location=" + point.lat + "," + point.lng + "&output=json&pois=1&ak=9zWs3Nt7FMrZDlqwUp3ZWm3W7pmrpGHI",
        dataType: 'jsonp',
        processData: false,
        type: 'get',
        success: function (sures) {
            var address = "";
            if (sures.status == 0)
                address = sures.result.formatted_address

            var ntime = new Date(time);

            var content = "车牌：" + platesNumber + "<br/>司机：" + name + "<br/>地址：" + address + "<br/>时间：" + top.dateFtt("yyyy-MM-dd hh:mm:ss", ntime);
            var myIcon = new BMap.Icon("/images/kache.png", new BMap.Size(80, 50))
            var marker = new BMap.Marker(point, { icon: myIcon });
            map.addOverlay(marker);
            var label = new BMap.Label(platesNumber, { offset: new BMap.Size(20, -10) });
            label.setStyle({
                color: "#fff",
                fontSize: "12px",
                height: "20px",
                lineHeight: "8px",
                fontFamily: "微软雅黑",
                backgroundColor: "#ff0000",
                maxWidth: "none",
                borderRadius: "5px",
                padding: "5px",
                opacity: "0.7"
            });
            marker.setLabel(label);
            marker.addEventListener("click", function (e) {
                openInfo(content, e)
            }
            );

        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert(XMLHttpRequest.status);
            alert(XMLHttpRequest.readyState);
            alert(textStatus);
        }
    });
}

function openInfo(content, e) {
    var p = e.target;
    var point = new BMap.Point(p.getPosition().lng, p.getPosition().lat);
    var infoWindow = new BMap.InfoWindow(content, opts);  // 创建信息窗口对象
    map.openInfoWindow(infoWindow, point); //开启信息窗口
}

function LoadMap(keyword) {
    $.getJSON("/Distribution/TrackData?keyword=" + keyword).done(function (data) {
        CommonAjaxCallback(data, function () {
            map.clearOverlays();
            if (data.result.length > 0) {
                for (var i = 0; i < data.result.length; i++) {
                    if (data.result[i].endCarMap != null) {
                        var point = new BMap.Point(data.result[i].endCarMap.longitude, data.result[i].endCarMap.latitude);
                        addMarker(point, data.result[i].platesNumber, data.result[i].carUser.name, data.result[i].carUser.creationTime);
                    }
                }
            }
        })
    });

}
$(document).ready(function () {
    LoadMap('');
    setInterval('LoadMap("")', 300000);
    $('#external-events').on('click', '.navy-bg', function () {
        var lng = $(this).data('lng');
        var lat = $(this).data('lat');
        var plates = $(this).data('plates');
        var caruser = $(this).data('caruser');
        var time = $(this).data('time');

        map.clearOverlays();
        map.centerAndZoom(lng + "," + lat, 11);

        var point = new BMap.Point(lng, lat);
        addMarker(point, plates, caruser, time);
    })

    $('#search').click(function () {
        var keyword = $('#keyword').val();
        LoadMap(keyword);
    });
})